<?php
namespace app\decorate\model;
use app\BaseModel;
use think\facade\Db;



class AdminArea extends BaseModel
{
    //定义表名 地区表
    protected $name = 'longbing_card_decorate_area';

    /**
     * @param $query
     * 查询范围
     */
    public function scopeStatus($query, $status){
        $query->where('status', '>', $status);
    }
    /**
     * @param $dis
     * @return array
     * @throws \think\exception\DbException
     * 地区列表
     */
    public function areaList($dis,$page = 10){
        $data = $this->where($dis)->status(-1)->order('sort desc')->paginate($page)->toArray();
        return $data;
    }

    /**
     * @param $data
     * @param $pId
     * @return array
     * 递归无限极
     */
    public function getTree($data, $pId,$l=0,$d=3,$type=0){
        $l++;
        $tree = array();
        if(!empty($data)){
            foreach($data as $k => $v) {
                if($v['pid'] == $pId&&$l<$d) {
                    $v['children'] = $this->getTree($data, $v['id'],$l,$d);
                    if(empty($v['children'])){
                        if($type!=1){
                            unset($v['children']);
                        }
                    }
                    $tree[]   = $v;
                }
            }
        }
        return $tree;
    }

    /**
     * User: chenniang
     * Date: 2019-10-12 16:43
     * @param $id
     * @param array $data
     * @return array
     * descption:转换回显格式
     */
    public function reset($id,&$data=array()){
        $cate = $this->areaInfo(['id'=>$id]);

//        dump($cate);
        if(!empty($cate)&&$cate['pid']!=$cate['id']){
            array_unshift($data,$cate['id']);
            $this->reset($cate['pid'],$data);
        }

        return $data;
    }


    /**
     * @author chenniang
     * @DataTime: 2020-12-02 11:31
     * @功能说明:递归
     */
    public function resetV2($id,&$data=array()){

        $data = array_merge($data,$id);

        $cate = $this->where('pid','in',$id)->column('id');

        if(!empty($cate)){

            $this->resetV2($cate,$data);
        }

        return array_unique($data);
    }

    /**
     * User: chenniang
     * Date: 2019-10-23 14:58
     * @param $id
     * @param $arr
     * @param int $l
     * @return array
     * descption:获取下级id
     */
    public function getSon($id,&$arr=array(),$l=0){
        $l++;
        $arr  = array_merge($id,$arr);
        $data = $this->where('pid','in',$id)->status(-1)->column('id');
        if(!empty($data)&&$l<4){
            $this->getSon($data,$arr,$l);
        }
        return $arr;
    }

    /**
     * User: chenniang
     * Date: 2019-10-12 09:54
     * @param $dis
     * @return mixed
     * descption:查询地区
     */
    public function areaSelect($dis,$d=3){
        $data = $this->where($dis)->status(-1)->order('sort desc,create_time desc')->select()->toArray();
        $data = $this->getTree($data,0,0,$d);
        return $data;
    }
    public function qareaSelect($dis,$d=3){
        $data = $this->where($dis)->status(-1)->order('sort desc,create_time desc')->select()->toArray();
        $data = $this->getTree($data,0,0,$d,1);
        return $data;
    }
    /**
     * @param $data
     * @return int|string
     * 添加地区
     */
    public function areaAdd($data){
        $data['create_time'] = time();
        $data['update_time'] = time();
        $data['status']      = 1;
        $res = $this->insert($data);
        return $res;
    }
    /**
     * @param $dis
     * @param $data
     * @return AdminCate
     * 编辑地区
     */
    public function areaUpdate($dis,$data){
        $data['update_time'] = time();
        $res = $this->where($dis)->update($data);
        return $res;

    }
    /**
     * @param $dis
     * @return AdminCate
     * 地区详情
     */
    public function areaInfo($dis){
        $data = $this->where($dis)->status(-1)->find();
        return !empty($data)?$data->toArray():$data;
    }

    /**
     * User: chenniang
     * Date: 2019-10-15 11:44
     * @param $dis
     * @return mixed
     * descption:区域名称
     */
    public function areaTitle($dis){
        $data = $this->where($dis)->status(-1)->value('title');
        return !empty($data)?$data:'暂无';
    }




}